package com.liliudong.cursormate.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liliudong.cursormate.common.constant.ConfigConst;
import com.liliudong.cursormate.common.exception.BusinessException;
import com.liliudong.cursormate.entity.SysPermission;
import com.liliudong.cursormate.entity.SysRole;
import com.liliudong.cursormate.mapper.SysPermissionMapper;
import com.liliudong.cursormate.mapper.SysRoleMapper;
import com.liliudong.cursormate.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author Liudong.li
 * @description 针对表【sys_role(用户角色)】的数据库操作Service实现
 * @createDate 2023-10-24 22:29:24
 */
@Service
@ConditionalOnProperty(name = {ConfigConst.CURSOR_MATE_POLICY}, havingValue = ConfigConst.CURSOR_MATE_POLICY_RULE)
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
        implements SysRoleService {

    protected final SysPermissionMapper sysPermissionMapper;

    @Override
    public List<String> getRoleList(Long loginId) {
        return baseMapper.selectRoleListByUserIdInterceptorIgnore(loginId);
    }

    @Override
    public SysRole getByIdIncludePermissions(Long id) {
        SysRole sysRole = baseMapper.selectById(id);
        if (Objects.isNull(sysRole)) {
            throw new BusinessException("角色不存在");
        }
        List<SysPermission> sysPermissionList = sysPermissionMapper.selectListByRoleId(id);
        sysRole.setSysPermissionList(sysPermissionList);
        return sysRole;
    }
}




